//
//  JFCreateBoardViewController.m
//  JobFree2.0
//
//  Created by Dale on 2017/5/31.
//  Copyright © 2017年 Dale. All rights reserved.
//

#import "JFCreateBoardViewController.h"
#import "JFCreateBoardCell.h"
#import "TTAddMemberViewController.h"

@interface JFCreateBoardViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,copy) NSString *board_name;
@property (nonatomic,copy) NSString *board_remark;
@property (nonatomic,copy) NSString *board_logo;

@end

@implementation JFCreateBoardViewController

- (NSArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = @[
                        @[@{@"type":@0,
                            @"name":@"",
                            @"description":@""}],
                        @[@{@"type":@1,
                            @"name":@"名称",
                            @"description":@"请输入名称"},
                          @{@"type":@1,
                            @"name":@"备注",
                            @"description":@"请输入备注"}],
                        @[@{@"type":@2,
                            @"name":@"成员",
                            @"description":@"1人",
                            @"members":@[[JFUser mainJFUser],  [[JFUser alloc] init]].mutableCopy}]
                        ];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"创建看板";
    [self configureLeftNavigationBarItem];
    [self configureRightNavigationBarItemWithTitle:@"创建"];
    [Common removeExtraCellLines:self.tableView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction)];
    [self.tableView addGestureRecognizer:tap];
}

- (void)handleLeftBtnAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)handleRightBtnAction {
    [self.view endEditing:YES];
    
    if ([Common isEmptyString:self.board_name]) {
        [super showText:@"请输入名称" afterSeconds:1.0];
        return;
    }
    if ([Common isEmptyString:self.board_remark]) {
        [super showText:@"请输入备注" afterSeconds:1.0];
        return;
    }
#warning to do...
    [super showHudWithText:@"正在创建"];
//默认会把创建看板的人员加入,如无人员加入,传空数组
    NSData *phasesData = [NSJSONSerialization dataWithJSONObject:@[@"to do", @"doing"] options:NSJSONWritingPrettyPrinted error:nil];
    NSString *phasesStr = [[NSString alloc] initWithData:phasesData encoding:NSUTF8StringEncoding];
    NSData *membersData = [NSJSONSerialization dataWithJSONObject:@[] options:NSJSONWritingPrettyPrinted error:nil];
    NSString *membersStr = [[NSString alloc] initWithData:membersData encoding:NSUTF8StringEncoding];
    NSDictionary *dic = @{@"board_name":self.board_name,
                          @"board_remark":self.board_remark,
                          @"board_logo":self.board_logo,
                          @"phases":phasesStr,
                          @"members":membersStr};
    NSLog(@"%@", dic);
    CreateBoardApi *api = [[CreateBoardApi alloc] initWithPara:dic];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"%@", request.responseObject);
        NSData *data = [request.responseObject[@"response"] dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if ([dic[CODE] intValue] == 200) {
            isRefrsh = YES;
            [super showHudWithText:dic[MESSAGE]];
            [super hideHudAfterSeconds:1.0];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [super showHudWithText:dic[MESSAGE]];
            [super hideHudAfterSeconds:1.0];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"%@", request.error);
        [super showHudWithText:request.error.localizedDescription];
        [super hideHudAfterSeconds:1.0];
    }];
}

- (void)handleTapAction {
    [self.view endEditing:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

//MARK: UITableViewDataSource && Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *secArr = self.dataSource[section];
    return secArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.section][indexPath.row];
    JFCreateBoardCell *cell = [JFCreateBoardCell cellWithTableView:tableView withDic:dic];
    cell.dic = dic;
    cell.textFieldBlock = ^(NSString *text, int type) {
        if (type == 0) {
            self.board_name = text;
        } else {
            self.board_remark = text;
        }
    };
    
    cell.dataSource = dic[@"members"];
    cell.clickMemberBlock = ^(JFCreateBoardCellType type) {
        if (type == JFCreateBoardCellTypePerson) {
            NSLog(@"点击头像");
        } else {
            TTAddMemberViewController *addMemberVC = [[TTAddMemberViewController alloc] initWithNibName:@"TTAddMemberViewController" bundle:nil];
            [self.navigationController pushViewController:addMemberVC animated:YES];
        }
    };
    
    CGFloat r = arc4random() % 256 / 255.0;
    CGFloat g = arc4random() % 256 / 255.0;
    CGFloat b = arc4random() % 256 / 255.0;
    NSString *board_logo = [NSString stringWithFormat:@"%.3f,%.3f,%.3f", r, g, b];
    cell.board_logo = board_logo;
    self.board_logo = board_logo;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.section][indexPath.row];
    return [JFCreateBoardCell loadCellHeightWithDic:dic];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return 20;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = kRGB(230, 230, 237);
        return view;
    }
    return nil;
}


@end
